package com.teach;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <pre>
 * - 
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-4-16 10:46:16
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 *
 */
public class Demo0201 {

	public static void main(String[] args) {
		System.out.println("430124198101070011:"+hasAdult("430124198101070011"));//true
		System.out.println("430124202101070011:"+hasAdult("430124202101070011"));//false
		
	}
	
	/**
	 * - 根据身份证号码判断是否成年
	 * @param idcard
	 * @return
	 */
	public static boolean hasAdult(String idcard) {
		if(!idcard.matches("^\\d{17}[\\dX]$")) {
			throw new RuntimeException("18位的身份证号码不正确");
		}
		String ymd = idcard.substring(6,14);
		System.out.println(ymd);
		SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
		try {
			Date d = f.parse(ymd);
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			c.add(Calendar.YEAR, 18);
			System.out.println(f.format(c.getTime()));
			return new Date().after(c.getTime());
		} catch (ParseException e) {
			throw new RuntimeException("18位的身份证号码不正确");
		}
	}
}
